package com.asohofrucol.web.util;

import org.apache.commons.codec.digest.DigestUtils;

public class CredentialsHelperUtil {

	public static int CODE_OK = 0;
	public static int WRONG_PASSWORD = -2;
	public static int CREDENTIAL_ERROR = -4;
	public static int DISABLED_USER = -1;

	public static int compareCredentials(String userPassword,
			String hashUserStoredPassword) {
		if (null != userPassword && hashUserStoredPassword != null) {
			String hashUserPassword = calculateHash(userPassword, userPassword);
			if (hashUserPassword.equals(hashUserStoredPassword)) {
				return CODE_OK;
			}
			return WRONG_PASSWORD;
		}
		return CREDENTIAL_ERROR;
	}
	
	public static String calculateHash(String data, String salt) {
		return DigestUtils.sha512Hex(data + salt);
	}
}
